package com.laogao.lesson050.mapper;

import com.laogao.lesson050.po.TestPO;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <b>description</b>： Java进阶、性能优化实战案例100讲，微信公众号:程序员恰恰，源码 & 文档 & 技术支持，请加个人微信号：it3306 <br>
 * <b>time</b>：2025/4/19 19:28 <br>
 * <b>author</b>：ready lao_gao
 */
public interface TestMapper {
    void _batchInsert(@Param("tableName") String tableName, @Param("list") List<TestPO> list);

    default void batchInsert(String tableName, List<TestPO> list) {
        int batchSize = 1000;
        int size = list.size();
        int batch = size % batchSize == 0 ? size / batchSize : size / batchSize + 1;
        for (int i = 0; i < batch; i++) {
            int fromIndex = i * batchSize;
            int toIndex = Math.min(fromIndex + batchSize, size);
            List<TestPO> insertList = list.subList(fromIndex, toIndex);
           // System.out.println("插入表：" + tableName + "，第 " + (i + 1) + " 批，数量：" + insertList.size());
            this._batchInsert(tableName, insertList);
        }
    }

    @Select("SELECT COUNT(*) FROM ${tableName}")
    Long count(@Param("tableName") String tableName);

    @Delete("delete from ${tableName}")
    void delete(@Param("tableName") String tableName);
}
